package online.sh.cn.fy.users.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import online.sh.cn.fy.common.utils.CookieUtil;
import online.sh.cn.fy.common.utils.ResponseUtils;
import online.sh.cn.fy.common.utils.TokenUtils;

/**
 * 统计产品剩余数量
 */
@WebServlet("/productcounts")
public class ProductCountServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String openid=CookieUtil.getCookieValueByName(request, "openid");
		String points=request.getParameter("points");
		JSONObject json=new JSONObject();
		if(null==openid || ""!=openid){
			TokenUtils tu=new TokenUtils();
			int counts=tu.CountProduct("电信宽带氮气加速瓶",points);
			if(9999-counts>0){
				counts=9999-counts;
			}else{
				counts=9999;
			}
            json.put("result", 0);
            json.put("counts", counts);
            ResponseUtils.renderJson(response, json.toString());
		}else{
			json.put("result", 1);
			ResponseUtils.renderJson(response, json.toString());
		}
	}
}
